﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace HelpCentral.Shared.Security
{
    public static class PasswordUtility
    {
        public static byte[] HashPassword(string password)
        {
            if (String.IsNullOrEmpty(password))
            {
                throw new Exception("Cannot hash null or empty password.");
            }

            var crypto = new SHA256CryptoServiceProvider();

            return crypto.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password), 0, password.Length);
        }

        public static bool ComparePassword(string password, byte[] passwordHash)
        {
            byte[] test = HashPassword(password);
            bool valid = true;

            if (test.Length == passwordHash.Length)
            {
                for (int i = 0; i < test.Length; i++)
                {
                    if (test[i] != passwordHash[i])
                    {
                        valid = false;
                        break;
                    }
                }
            }
            else
            {
                valid = false;
            }

            return valid;
        }
    }
}
